package com.cheyun.netsalev3.utils

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.BR
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.view.ForgetActivity
import com.cheyun.netsalev3.view.LoginActivity
import com.cheyun.netsalev3.view.MainActivity
import com.cheyun.netsalev3.view.RegisterActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.widget.floatingview.FloatingView
import com.gyf.immersionbar.ImmersionBar


/**
 * @author Michael
 * @date  2018/11/14 - 14:52.
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {
    lateinit var binding: T
    private lateinit var viewModel: ViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //今日头条适配方案
        ScreenAdaptUtil.setCustomDensity(this, application)
        setStatusMode()
        NavigationBarUtil.hideNavigationBar(window)

        binding = DataBindingUtil.setContentView(this, initContentView())
        viewModel = if (initViewModel() == null) {
            ViewModelProviders.of(this).get(BaseViewModel::class.java)
        } else {
            initViewModel()!!
        }
        binding.setVariable(initVariableId(), viewModel)
        binding.setLifecycleOwner(this)
        initActivity()
        initView()
        initData()

//        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){
//            //透明状态栏
//            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//            //透明导航栏
//            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
//        }
        if (this is MainActivity) {
            ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(false).statusBarColor(R.color.colorbg_blue)
                    .fitsSystemWindows(true)
                    .init()
        }
//        else if(this is LoginActivity || this is RegisterActivity || this is ForgetActivity) {
//            ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(true).statusBarColor(R.color.white)
//                    .fitsSystemWindows(true)
//                    .init()
//        }
        else{
            ImmersionBar.with(this).keyboardEnable(true).statusBarDarkFont(true).statusBarColor(R.color.colorbg)
                    .fitsSystemWindows(true)
                    .init()
        }

//        if (this is LoginActivity ||
//                this is RegisterActivity ||
//                this is Forget1Activity ||
//                this is CodeCheckActivity ||
//                this is ForgetActivity
//        ) {
//            ImmersionBar.with(this)
//                    .fitsSystemWindows(true)
//                    .statusBarDarkFont(true)
//                    .init()
//        }else if(this is GuideActivity) {
//            ImmersionBar.hideStatusBar(window)
//        }
//        else if(this is ShowroomMainActivity ||
//                this is MonitorDetailActivity ||
//                this is AddDeviceNumActivity ||
//                this is PatrolProjectActivity ||
//                this is PatrolDetailActivity ||
//                this is PatrolSuccessActivity ||
//                this is DealerPatrolDetailActivity ||
//                this is PatrolChooseDealerActivity ||
//                this is DealerPatrolExplanActivity ||
//                this is SeriesNumSearchActivity ||
//                this is AutoWifiPrepareStepOneActivity ||
//                this is AutoWifiNetConfigActivity ||
//                this is ConfigWifiExecutingActivity ||
//                this is AutoWifiResetActivity ||
//                this is CustomPatrolRecReplyEditActivity ||
//                this is CustomPatrolDetailActivity ||
//                this is FaceUserActivity){
//            ImmersionBar.with(this).statusBarColor(R.color.xzt_blue)
//                    .fitsSystemWindows(true)
//                    .init()
//            //
//        }else if(this is DealerChartsActivity || this is ChartFilter2Activity){
////            ImmersionBar.with(this).fitsSystemWindows(false).init()
//            ImmersionBar.hideStatusBar(window)
//        }
//        else{
//            ImmersionBar.with(this).statusBarColor(R.color.blue)
//                    .fitsSystemWindows(true)
//                    .init()
//        }
    }

    override fun finish() {
        super.finish()
        MyApplication.getInstance().removeActivity(this::class.java)
    }


    fun MyToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }

    private fun initActivity() {
        MyApplication.getInstance().addActivity(this)
        if (BaseViewModel::class.java.isInstance(viewModel)) {
            var baseViewModel = viewModel as BaseViewModel
            //公用Toast 用于消息提示
            baseViewModel.toast.observe(this, Observer {
                Log.e("acitivityToast", "$it:$this")
                MyToast(it)
            })
            //跳转
            baseViewModel.intent.observe(this, Observer {
                it.setClass(this, it.cls)
                startActivity(it)
            })
            //关闭activity
            baseViewModel.finish.observe(this, Observer {
                finish()
            })
            baseViewModel.finishWithBack.observe(this, Observer {
                setResult(it, intent)
                finish()
            })

            //请求错误通知
//            LiveEventBus.get()
//                    .with("ApiCallback", BaseResp::class.java)
//                    .observe(this, Observer {
//                        if (it.skey == "authover") {
//                            //跳转登陆
//                            val i2 = Intent(this, MainActivity::class.java)
//                            i2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                            i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
//                            startActivity(i2)
//                        }
//                        Toast.makeText(this, it.msg, Toast.LENGTH_LONG).show()
//                    })
        }

//        val floatingManage = FloatingManage(this)
//        floatingManage.add()

//        FloatingManage.get(this).add()
    }


    /**
     * 返回布局id
     */
    abstract fun initContentView(): Int

    /**
     * 返回ViewMode
     */
    open fun initViewModel(): ViewModel? {
        return null
    }

    /**
     * 返回绑定的VariableId 默认viewMode 可以覆盖重写
     */
    open fun initVariableId(): Int {
        return BR.viewModel
    }

    /**
     * 设置状态栏字体颜色默认黑色
     */
    open fun setStatusMode() {
//        ScreenAdaptUtil.StatusBarLightMode(this)
    }

    /**
     * 初始化view
     */
    abstract fun initView()

    /**
     * 初始化data
     */
    abstract fun initData()

    override fun onStart() {
        super.onStart()
        FloatingView.get().attach(this);
    }

    override fun onStop() {
        super.onStop()
        FloatingView.get().detach(this);
    }


}